package reittiopas;


import javax.microedition.rms.*;

import java.util.Enumeration;
import java.util.Vector;
import java.io.*;

public class RecordManager {

   private RecordStore recordStore = null;
   private String fileName;
       
   public RecordManager() {}

   public RecordManager(String fileName) {
	   this.fileName = fileName;
   }
   
   private void open() {
	   try {
	        this.recordStore = RecordStore.openRecordStore(this.fileName, true);
	      } catch(Exception rse) {
	        rse.printStackTrace();
	      }
   }

   private void close() throws RecordStoreNotOpenException,
                               RecordStoreException {
        if (recordStore.getNumRecords() == 0) {
            String fileName = recordStore.getName();
            recordStore.closeRecordStore();
            recordStore.deleteRecordStore(fileName);
        } else {
            recordStore.closeRecordStore();
        }
    }
   
   /**
    * 
    * @param records
    */
   public void setRecords(Vector records)
   {
	   this.open();
	   ByteArrayOutputStream ostrmBytes  = new ByteArrayOutputStream();
	   DataOutputStream ostrmDataType  = new DataOutputStream(ostrmBytes);
     try
     {
       byte[] record;
       int recordsAlreadySaved = recordStore.getNumRecords();

       for (int i = 0; i < records.size(); i++)
       {
         String text = (String)records.elementAt(i);
         ostrmBytes.reset();
         ostrmDataType.writeUTF(text);
         ostrmDataType.flush();

         // Get stream data into byte array and write record
         record = ostrmBytes.toByteArray();
         if(i >= recordsAlreadySaved)
        	 recordStore.addRecord(record, 0, record.length);
         else
        	 recordStore.setRecord(i, record, 0, record.length);
         
         this.close();
       }
     }
     catch (Exception e)
     {
       e.printStackTrace();
     }
   }
   
   public void clearRecords() {
	   try{
         RecordStore.deleteRecordStore(this.fileName);
       }catch (Exception e){e.printStackTrace();}
   }
   
   public void addRecord(String data) {

  	 try{
  		 RecordStore recordStore = RecordStore.openRecordStore(this.fileName, true );
  	      
  	        String outputData = data;
  	        byte[] byteOutputData = outputData.getBytes();
  	        recordStore.addRecord(byteOutputData, 0, 
  	        byteOutputData.length);
  	      
  	        byte[] byteInputData = new byte[1]; 
  	        int length = 0;
  	        
  	        for (int x = 1; x <= recordStore.getNumRecords(); x++){
  	          if (recordStore.getRecordSize(x) > byteInputData.length){
  	            byteInputData = new byte[recordStore.getRecordSize(x)];
  	          }
  	          length = recordStore.getRecord(x, byteInputData, 0);

  	        }  	      
  	        recordStore.closeRecordStore();
  	      }catch (Exception e){}

//  	      if(RecordStore.listRecordStores() != null){
//  	        try{
//  	          RecordStore.deleteRecordStore("Reittiopastesti1");
//  	        }catch (Exception e){}
//  	      }      

  }


//   public synchronized void addRecord(String record) {
//	this.open();
//	ByteArrayOutputStream baos = new ByteArrayOutputStream();
//	DataOutputStream outputStream = new DataOutputStream(baos);
//	try {
//	    outputStream.writeUTF(record);
//	}
//	catch (IOException ioe) {
//	    System.out.println(ioe);
//	    ioe.printStackTrace();
//	}
//	byte[] b = baos.toByteArray();
//	try {
//	    recordStore.addRecord(b, 0, b.length);
//	    this.close();
//	}
//	catch (RecordStoreException rse) {
//	    System.out.println(rse);
//	    rse.printStackTrace();
//	}
//    }

    private synchronized RecordEnumeration enumerate() throws RecordStoreNotOpenException {
       return recordStore.enumerateRecords(null, null, false);
    }
    
//    public Vector getRecords() {
//    	Vector res = new Vector();
//      try {
//         RecordEnumeration re = this.enumerate();
//         while(re.hasNextElement()) {
//           String record = new String(re.nextRecord());
//           res.addElement(record);
//         }
//      } catch(NullPointerException ex) { return new Vector();} catch (RecordStoreNotOpenException e) {
//		e.printStackTrace();
//	} catch (RecordStoreException e) {
//		e.printStackTrace();
//	}
//     return res;
//   }

    public Vector getRecords() {
    	Vector res = new Vector();
     	 try{
     		 RecordStore recordStore = RecordStore.openRecordStore(this.fileName, true );
     	      
     	        byte[] byteInputData = new byte[1]; 
     	        int length = 0;
     	        
     	        for (int x = 1; x <= recordStore.getNumRecords(); x++){
     	          if (recordStore.getRecordSize(x) > byteInputData.length){
     	            byteInputData = new byte[recordStore.getRecordSize(x)];
     	          }
     	          length = recordStore.getRecord(x, byteInputData, 0);
     	          res.addElement(new String(byteInputData, 0, length));
     	        }  	      
     	        recordStore.closeRecordStore();
     	      }catch (Exception e){}

//     	      if(RecordStore.listRecordStores() != null){
//     	        try{
//     	          RecordStore.deleteRecordStore("Reittiopastesti1");
//     	        }catch (Exception e){}
//     	      }      
     	      return res;
    	
    }

}
